var home = require("../../utils/util.js");
var url = home.home;
var url2 = home.home2;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    value: "",
    color: "color",
    bgcolor: home.getKey('bgcolor'),
    activeKey: 0,
    page: 1, //分页
    type: 1,
    cate: [],
    listData: [], //列表数据
    userId: home.getKey('userId'), //会员id
    cate_id: '', //分类id
    //tabbar: ["热门", "娱乐", "体育", "国内", "财经", "科技", "教育", "汽车"],
    currentTab: 0, //预设当前项的值
    scrollLeft: 0, //tab标题的滚动条位置
    state: wx.getStorageSync('state'),
    states: false,
    btnState:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      states:false 
    });
  },

  /**
   * 跳转预约详情
   */
  toGoods(e) {
    let goodsId = e.currentTarget.dataset.id;
    let info = {};
    for (let i = 0; i < this.data.listData.length; i++) {
      if (this.data.listData[i].projectId == goodsId) {
        info = this.data.listData[i];
      }        
    }
    wx.navigateTo({
      url: '../../shop/yuyue/index?info='+JSON.stringify(info),
    })
  },

  /**
   * 切换sidebar
   */
  sidebar(e) {
    let cur = e.currentTarget.dataset.current;
    let cate_id = this.data.cate_id;
    //cate_id = this.data.cate[e.detail].id;
    cate_id = this.data.cate[cur].id;
    // let cate_id = this.data.cate[e.detail].id;
    console.log(cate_id)
    if (this.data.currentTab == cur) {
      return false;
    } else {
      this.setData({
        currentTab: cur,
        //activeKey: e.detail,
        activeKey: cur,
        page: 1,
        cate_id: cate_id,
      })
      this.getShopList();
    }
  },

  //获取预约分类
  getCommodityCate() {
    let that = this;
    wx.request({
      url: url + 'app/prebookGroup',
      data: {
        storeId: home.getKey('storeId')
      },
      method: 'GET',
      header: {
        "content-type": "application/json", // 默认
      },
      success: function (res) {
        if (res.data.code == 200) {
          let json = {}
          // let json = {
          //   id: 1,
          //   title: '商品',
          //   parent: 0
          // };
          let CommodityGroup = [{id:0,name:'全部',parent:0}];
          for (let i = 0; i < res.data.result.length; i++) {
            res.data.result[i].parent = 1;
            CommodityGroup.push(res.data.result[i]);
          }
          //CommodityGroup.unshift(json);
          let cate_id = that.data.cate_id;
          if (that.data.cate_id == '') {
            cate_id = CommodityGroup[0].id;
          }
          that.setData({
            cate: CommodityGroup,
            cate_id: cate_id
          });
          that.getShopList();
        } else {
          home.wxMessage(res.data.message, 'none');
        }
      }
    })
  },



  //获取列表
  getShopList() {
    let requestUrl = '';
    let groupId = '';
    requestUrl = url + 'app/prebookGroupDetail'
    groupId = this.data.cate_id;
    let that = this;
    wx.request({
      url: requestUrl,
      data: {
        storeId: home.getKey('storeId'),
        pageNo: that.data.page,
        groupId: groupId,
        memberId: home.getKey('userId'),
        keyword: that.data.value
      },
      method: 'GET',
      header: {
        "content-type": "application/json", // 默认
      },
      success: function (res) {
        if (res.data.code == 200) {
          let listData = that.data.listData;
          if (that.data.page == 1) {
            for (let i = 0; i < res.data.result.length; i++) {
              let pics = '';
              if (that.data.type == 1) {
                if (res.data.result[i].productPic) {
                  pics = res.data.result[i].productPic.split(',');
                  if (pics != '') {
                    res.data.result[i].pic = 'https://kyz.fbhkj.com//' + pics[0];
                  }
                }
              } else {
                if (res.data.result[i].projectPic) {
                  pics = res.data.result[i].projectPic.split(',');
                  if (pics != '') {
                    res.data.result[i].pic = 'https://kyz.fbhkj.com//' + pics[0];
                  }
                }
              }
            }
            listData = res.data.result;
          } else {
            for (let i = 0; i < res.data.result.length; i++) {
              let pics = '';
              if (that.data.type == 1) {
                if (res.data.result[i].productPic) {
                  pics = res.data.result[i].productPic.split(',');
                  if (pics != '') {
                    res.data.result[i].pic = 'https://kyz.fbhkj.com//' + pics[0];
                  }
                }
              } else {
                if (res.data.result[i].projectPic) {
                  pics = res.data.result[i].projectPic.split(',');
                  if (pics != '') {
                    res.data.result[i].pic = 'https://kyz.fbhkj.com//' + pics[0];
                  }
                }
              }
              listData.push(res.data.result[i]);
            }
          }
          that.setData({
            listData: listData
          });
        } else {
          home.wxMessage(res.data.message, 'none');
        }
      }
    })
  },

  //搜索
  onSearch(e){
    this.setData({
      value:e.detail
    })
    this.getShopList();
  },

  onClear(e){
    this.setData({
      value:''
    })
    this.getShopList();
  },

  scClick() {
      wx.reLaunch({
          url: '../shop/index',
      })
  },
  // 滚动切换标签样式
  switchTab: function(e) {
    let that = this;
    that.setData({
      currentTab: e.detail.current
    });
    that.checkCor();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      value:"",
      userId:home.getKey('userId'),
      page:1,
    })
    if (home.getKey('state') == 1) {
      this.setData({
        state: 1
      });
    } else if (home.getKey('state') == 2) {
      this.setData({
        state: 2
      });
    }
    this.getCommodityCate();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // let page = this.data.page;
    // this.setData({
    //   page: ++page
    // });
    // this.getShopList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})